package com.hdoit.kylin.web.baseInfo.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.baseInfo.model.MoneyType;
import com.hdoit.kylin.logical.baseInfo.service.IMoneyTypeService;
import com.hdoit.kylin.logical.viewmodel.baseInfo.MoneyTypeSearch;

/**
* @author lufu
* @email
* @createtime 2017年07月25日 16:00:37
* @comment
*/
@Controller
@RequestMapping(value = "/admin/baseInfo/moneyType/")
public class MoneyTypeController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/baseInfo/moneyType/";
	}
	
	private IMoneyTypeService moneyTypeService;
	
	public IMoneyTypeService getMoneyTypeService() {
		return moneyTypeService;
	}
	
	@Resource
	public void setMoneyTypeService(IMoneyTypeService moneyTypeService) {
		this.moneyTypeService = moneyTypeService;
	}
	
	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String detailedViewName="moneyTypeDetailed";
	private final String saveViewName="save";
	private final String deleteViewName = "delete";
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model,PagerModel pagerModel,MoneyTypeSearch search,boolean ajaxPost){
		pagerModel.setTotalAmount(moneyTypeService.queryCount(search));
		List<MoneyType> moneyTypes= moneyTypeService.queryList(search,pagerModel);
		
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("moneyTypes", moneyTypes);
		model.addAttribute("moneyTypeSearch", search);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
	public String moneyTypeDetailed(Model model,String key){
		MoneyType moneyType=new MoneyType();
		if(!StringUtils.isEmpty(key)){
			moneyType=moneyTypeService.queryModel(key);
		}
		
		model.addAttribute("moneyType", moneyType);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST})
	public String save(Model model,MoneyType moneyType){
		if(StringUtils.isEmpty(moneyType.getId())){
			moneyType.setId(CommonUtils.getKeyId());
			/*moneyType.setCreateDate(DateUtils.getCurrent());
			moneyType.setDeleteMark(YesOrNoInteger.no);
			moneyType.setCreateUserId(getLoginModel().getLoginId());
			moneyType.setCreateUserName(getLoginModel().getLoginName());*/
			moneyType.setModelStatus(ModelStatus.add);
		}else{
			/*moneyType.setModifyDate(DateUtils.getCurrent());
			moneyType.setModifyUserId(getLoginModel().getLoginId());
			moneyType.setModifyUserName(getLoginModel().getLoginName());*/
			moneyType.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if(moneyTypeService.applyChange(moneyType)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("moneyType", moneyType);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
	public @ResponseBody String delete(String key){
		if(moneyTypeService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}